package com.zztl.infiniteBand.util

import android.util.Log
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by lsm on 2016/10/27.
 */

object DateUtils {

    /**
     * 获取当月的 天数
     */
    val currentMonthDay: Int
        get() {

            val a = Calendar.getInstance()
            a.set(Calendar.DATE, 1)
            a.roll(Calendar.DATE, -1)
            val maxDate = a.get(Calendar.DATE)
            return a.get(Calendar.DATE)
        }

    /**
     * 是否在领取奖励的这段时间内
     */
    //        if (DateUtils.isInTime("06:00-12:00", nowTime)) {
    val isInReceiveTime: Boolean
        get() {
            val nowTime = SimpleDateFormat("HH:mm").format(Date())
            return DateUtils.isInTime("08:00-12:30", nowTime) || DateUtils.isInTime("14:00-18:00", nowTime)
        }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    fun getDaysByYearMonth(year: Int, month: Int): Int {

        val a = Calendar.getInstance()
        a.set(Calendar.YEAR, year)
        a.set(Calendar.MONTH, month - 1)
        a.set(Calendar.DATE, 1)
        a.roll(Calendar.DATE, -1)
        return a.get(Calendar.DATE)
    }

    /**
     * 根据日期 找到对应日期的 星期
     */
    fun getDayOfWeekByDate(date: String): String {
        var dayOfweek = "-1"
        try {
            val myFormatter = SimpleDateFormat("yyyy-MM-dd")
            val myDate = myFormatter.parse(date)
            val formatter = SimpleDateFormat("E")
            val str = formatter.format(myDate)
            dayOfweek = str

        } catch (e: Exception) {

        }

        return dayOfweek
    }

    /**
     * 判断某一时间是否在一个区间内
     *
     * @param sourceTime 时间区间,半闭合,如[10:00-20:00)
     * @param curTime    需要判断的时间 如10:00
     * @return
     * @throws IllegalArgumentException
     */
    fun isInTime(sourceTime: String?, curTime: String?): Boolean {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw IllegalArgumentException("Illegal Argument arg:" + sourceTime!!)
        }
        if (curTime == null || !sourceTime.contains("-") || !curTime.contains(":")) {
            throw IllegalArgumentException("Illegal Argument arg:" + curTime!!)
        }

        val args = sourceTime.split("-".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
        val sdf = SimpleDateFormat("HH:mm")
        try {
            val now = sdf.parse(curTime).time
            val start = sdf.parse(args[0]).time
            val end = sdf.parse(args[1]).time
            if (args[1] == "00:00") {
                args[1] = "24:00"
            }
            return if (end < start) {
                now !in end..(start - 1)
            } else {
                now in start..(end - 1)
            }
        } catch (e: ParseException) {
            e.printStackTrace()
            throw IllegalArgumentException("Illegal Argument arg:$sourceTime")
        }

    }

    fun compareDate(DATE1: String, DATE2: String): Int {
        val df = SimpleDateFormat("yyyy-MM-dd")
        try {
            val dt1 = df.parse(DATE1)
            val dt2 = df.parse(DATE2)
            return if (dt1.time > dt2.time) {
                1
            } else if (dt1.time < dt2.time) {
                -1
            } else {
                0
            }
        } catch (exception: Exception) {
            exception.printStackTrace()
        }

        return 0
    }

    fun getTime(date: Date): String {
        return SimpleDateFormat("HH:mm:ss").format(date)
    }

    fun formatTime(date: Date): String {
        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)
    }

    fun formatNoSecoondTime(date: Date): String {
        return SimpleDateFormat("yyyy-MM-dd HH:mm").format(date)
    }

    fun formatDayTime(date: Date): String {
        return SimpleDateFormat("yyyy-MM-dd").format(date)
    }

    fun formatMonthTime(date: Date): String {
        return SimpleDateFormat("yyyy-MM").format(date)
    }

    fun formatYearTime(date: Date): String {
        return SimpleDateFormat("yyyy").format(date)
    }

    fun formatMonthDayTime(date: Date): String {
        return SimpleDateFormat("MM-dd").format(date)
    }

    fun formatNoYearTime(date: Date): String {
        return SimpleDateFormat("MM-dd HH:mm:ss").format(date)
    }

    fun formatNoYearAndSecondsTime(date: Date): String {
        return SimpleDateFormat("MM-dd HH:mm").format(date)
    }


    //获取当前日期的前一天
    fun getLastDay(date: Date): Date {
        var date = date
        val calendar = Calendar.getInstance()
        calendar.time = date
        calendar.add(Calendar.DAY_OF_MONTH, -1)
        date = calendar.time
        return date
    }

    //获取当前日期的前一天
    fun getLastDayTime(date: Date): Date {
        var date = date
        val calendar = Calendar.getInstance()
        calendar.time = date
        calendar.add(Calendar.DAY_OF_MONTH, -1)
        calendar.add(Calendar.HOUR, 21)
        date = calendar.time
        return date
    }


    fun getDateFromString(dateStr: String, pattern: String): Date? {
        var resDate: Date? = null
        try {
            resDate = SimpleDateFormat(pattern).parse(dateStr)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return resDate
    }

    /**
     * 将长时间格式字符串转换为字符串 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    fun longToStrng(time: Int): String {
        val hou: String
        val min: String
        val hour = time / 60
        val minute = time - hour * 60
        if (hour < 10) {
            hou = "0$hour"
        } else {
            hou = hour.toString()
        }
        if (minute < 10) {
            min = "0$minute"
        } else {
            min = minute.toString()
        }

        return "$hou:$min"
    }


    fun int2Byte(num: Int): ByteArray {
        val string = Integer.toBinaryString(num)
        val bytes = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0)

        for (i in 0 until string.length) {
            val iii = Integer.parseInt(string.substring(i, i + 1))
            bytes[string.length - 1 - i] = iii.toByte()
        }
        return bytes
    }

    fun byteToint(bytes: ByteArray): Int {
        var result: Int

        var r = 0
        for (i in bytes.indices) {
            r += (bytes[i] * Math.pow(2.0, i.toDouble())).toInt()
        }
        result = r
        Log.d("", "" + result)
        return result
    }

    fun gethour(time: Int): Int {
        return time / 60

    }

    fun minute(time: Int): Int {
        val hour = time / 60
        return time - hour * 60

    }


    fun byteSum(bytes: ByteArray): Int {
        var total = 0
        for (i in bytes.indices)
            total += bytes[i].toInt()
        return total
    }


    fun getTimeMillis(time: String): Long {
        try {
            val dateFormat = SimpleDateFormat("yy-MM-dd HH:mm:ss")
            val dayFormat = SimpleDateFormat("yy-MM-dd")
            val curDate = dateFormat.parse(dayFormat.format(Date()) + " " + time)
            return curDate.time
        } catch (e: ParseException) {
            LogUtils.i("swl", "异常" + e.message)
            e.printStackTrace()
            return 0
        }

    }
}
